﻿using System;
using System.Threading;

namespace ProcessBarDemo.UpdateFramework
{
    public static class ProgressReporter
    {
        private static long _total;
        private static long _downloaded;
        private static readonly object _lock = new object();
        private static DateTime _lastUpdate = DateTime.MinValue;

        public static void Init(long total)
        {
            _total = total;
            _downloaded = 0;
        }

        public static void Report(int bytes)
        {
            Interlocked.Add(ref _downloaded, bytes);
            lock (_lock)
            {
                if (_downloaded >= _total || (DateTime.Now - _lastUpdate).TotalMilliseconds >= 100)
                {
                    _lastUpdate = DateTime.Now;
                    double percent = _downloaded * 100.0 / _total;
                    DownloadService.process = percent;
                }
            }
        }
    }
}
